package com.example.ad_project;



import java.util.List;

import com.google.gson.Gson;
import com.nus.iss.ad.dbhelper.RequestDBHelper;
import com.nus.iss.ad.model.Request;
import com.nus.iss.ad.model.RequestDetail;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class ApproveRequest extends BaseActivity
implements OnItemClickListener
{
	private static final int APPROVE_REQUEST_DETAIL_REQUEST=800;
	RequestDBHelper data=new RequestDBHelper();
	List<Request> reqList=null;

	@Override
	public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	    setContentView(R.layout.approve_request);
	    
	    reqList=data.getPendingRequest();
	    Request[] reqarr=new Request[reqList.size()];
	    reqarr=reqList.toArray(reqarr);
	    
	    RequestAdapter ra=new RequestAdapter(this,reqarr);
	    
	    ListView lv= (ListView) findViewById(R.id.listViewRequest);
	    lv.setAdapter(ra);
	    
	    lv.setOnItemClickListener(this);
	  
}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
		// TODO Auto-generated method stub
		Intent intent=new Intent(this,ApproveRequestDetailActivity.class);
		Request r= reqList.get(position);
		
		Gson gson=new Gson();
		String request=gson.toJson(r,Request.class);
		
		intent.putExtra("request",request);
		startActivityForResult(intent, APPROVE_REQUEST_DETAIL_REQUEST);
	}
	
	protected void onActivityResult
    (int requestCode, int resultCode) {
if (resultCode == RESULT_OK && requestCode == APPROVE_REQUEST_DETAIL_REQUEST) {
//refresh by restarting
recreate();
		}
	}
}